﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using YQBlog.General;

namespace YQBlog.Controllers
{
    public class LangController : Controller
    {
        GeneralConfigInfo configinfo = YQBlog.General.WebUtils.configinfo;

        /// <summary>
        /// 切换到指定语言，跳转到来源页新的语言页面url
        /// </summary>
        public ActionResult ChangeLanguage(string language)
        {
            string lang = language == "zh-cn" ? "" : language;
            Session["CurrentLanguage"] = new CultureInfo(lang);

            if (Request.UrlReferrer.AbsoluteUri.ToLower().Contains("admin"))
                return this.Redirect(Request.UrlReferrer.ToString());
            else
            {
                string langStr = language.ToLower() != configinfo.DefaultLang ? "/" + language : "";
                string referrerPath = WebUtils.Langs.Aggregate(this.Request.UrlReferrer.PathAndQuery, (current, c) => current.Replace(c + "", ""));
                string url = WebUtils.MyString(langStr + referrerPath,"/");
                if (referrerPath.Split('/')[1] == configinfo.WebStaticFolderPart1)
                {
                    referrerPath = referrerPath.Replace("/"+configinfo.WebStaticFolderPart1, "");
                    url = string.IsNullOrWhiteSpace(langStr + referrerPath) ? "/" :"/"+ configinfo.WebStaticFolderPart1 + langStr + referrerPath;
                }
                return this.Redirect(url);
            }
        }

    }
}
